#include <jni.h>
#include <android/log.h>
#include <malloc.h>
#include <string.h>

#include "Log.h"
#include "BMP.h"
#define LOG_TAG "xx_Result"

#ifndef CLASSNAME
#error CLASSNAME was not defined, please define CLASSNAME first like -DCLASSNAME=com_alvasystems_fr_FRJni
#endif

#define JNIFUNC_NOTUSE2(CLZ, FUN) JNIEXPORT JNICALL Java_##CLZ##_##FUN
#define JNIFUNC_NOTUSE1(CLZ, FUN) JNIFUNC_NOTUSE2(CLZ, FUN)
#define JNIFUNC(FUNCNAME) JNIFUNC_NOTUSE1(CLASSNAME, FUNCNAME)

extern "C" {

static char modelName[200];
static char modelName1[200];
jint
       JNIFUNC(FRInit)(JNIEnv * env, jobject obj, jstring modulePath, jstring modelPath1){


           const char* str;
           const char * str1;
           jboolean fa = false;
           str = env->GetStringUTFChars(modulePath, &fa);
           str1 = env->GetStringUTFChars(modelPath1, &fa);
           strcpy(modelName, str);
           strcpy(modelName1, str1);
           LOGE("*********** %s", modelName1);
           IMAGE out_img;

          int err = 0; //LoadBMP(modelName, &out_img, BIT32);
           if(err != 0)
           {
        	  // LOGE("LoadBMP err %d \n", err);
           }

           //WriteBMP(modelName1, &out_img, BIT32);

           return 0;
       }

jint
      JNIFUNC(FRPerform)(JNIEnv * env, jobject obj, jint width, jint height, jintArray previewARGB, jintArray resultARGB){


          unsigned int * data = NULL;
          data =(unsigned int *) env->GetIntArrayElements(previewARGB, 0);
          unsigned int * result = (unsigned int *)env->GetIntArrayElements(resultARGB, 0);
          unsigned char * resultChar = (unsigned char *)result;

          LOGE("width = %d height = %d", width, height);
          for(int i = 0; i < height; i ++)
			{
				for(int j = 0; j < width; j ++)
				{

					unsigned char a = (data[i * width + j] & 0xFF000000) >> 24;
					unsigned char r = (data[i * width + j] & 0x00FF0000) >> 16;
					unsigned char g = (data[i * width + j] & 0x0000FF00) >> 8;
					unsigned char b = (data[i * width + j] & 0x000000FF);

					unsigned char t_grey = (unsigned char)(0.299f * r + 0.587f * g + 0.114f * b);
					resultChar[(i * width + j) * 4 + 0] = t_grey;
					resultChar[(i * width + j) * 4 + 1] = t_grey;
					resultChar[(i * width + j) * 4 + 2] = t_grey;
					resultChar[(i * width + j) * 4 + 3] = 255;

				}
			}

          env->ReleaseIntArrayElements(previewARGB,(jint*)data,0);
          env->ReleaseIntArrayElements(resultARGB,(jint*)result,0);

          LOGE("width = %d height = %d *****333333333333", width, height);
          return 0;
      }


#if 0
    jint 
    JNIFUNC(FRGetFaceNum)(JNIEnv * env, jobject obj){
       int faceNum = 0; //FRGetFaceNum();
        return faceNum;
    }

    jfloatArray
    JNIFUNC(FRGetFaceDiscriptions)(JNIEnv * env, jobject obj, jint index){
        float * data = NULL; //FRGetFaceDiscriptions(index);

        jfloatArray facePoints = env->NewFloatArray(70*2);

        env->SetFloatArrayRegion(facePoints, 0,  70*2 ,data);

        return facePoints;
    }
    jint
       JNIFUNC(FRInit)(JNIEnv * env, jobject obj, jstring modulePath, jstring initPointsPath){

           //FRInit(modulePath, initPointsPath);
           const char* str;
           const char * strSharp;
           char modelName[1024];
           char shapeName[1024];

           jboolean fa = false;
           str = env->GetStringUTFChars(modulePath, &fa);

           strSharp = env->GetStringUTFChars(initPointsPath, &fa);

           strcpy(modelName, str);
           strcpy(shapeName, strSharp);
           //LOGE("FRInit before..........................");
           FRInit(modelName, Type_File, shapeName);
           //LOGE("FRInit after..........................");
           return 0;
       }

       jint
       JNIFUNC(FRSetDataInfo)(JNIEnv * env, jobject obj, jint width, jint height, jint format, jint oraction){

           //LOGE("FRSetDataInfo before..........................");
           int err = 0;
           err =  FRSetDataInfo(width, height, format, oraction);
           if(0 != err)
           {
               LOGE("FRSetDataInfo err ");
           }
           //LOGE("FRSetDataInfo after..........................");
           return 0;
       }

       jint
       JNIFUNC(FRPerform)(JNIEnv * env, jobject obj, jint faceNumber, jfloatArray boxpoints, jbyteArray previewYuv){

           float * edgePoints = NULL;
           int err = 0;

           unsigned char * data = NULL;
           int faceNum = 0;
           int i = 0;

           int width = 0;
           int height = 0;
           int isOver = 0;


           faceNum = faceNumber;

           edgePoints = env->GetFloatArrayElements(boxpoints, 0);


           data =(unsigned char *) env->GetByteArrayElements(previewYuv, 0);

           err = FRPerform(faceNum, edgePoints, data);

           if(0 != err)
           {
               env->ReleaseByteArrayElements(previewYuv,(jbyte*)data,0);
               env->ReleaseFloatArrayElements(boxpoints, (jfloat *)edgePoints, 0);
               return -1;
           }
           env->ReleaseFloatArrayElements(boxpoints, (jfloat *)edgePoints, 0);
           env->ReleaseByteArrayElements(previewYuv,(jbyte*)data,0);
           return 0;
       }

       jint
       JNIFUNC(FRUnit)(JNIEnv * env, jobject obj){


       }
#endif
}
